package vn.allmobile.newsreader.activities.main;

import java.util.List;

import vn.allmobile.newsreader.R;
import vn.allmobile.newsreader.data.Paper;
import vn.allmobile.newsreader.utils.Contants;
import vn.allmobile.newsreader.utils.ImageLoader;
import vn.allmobile.newsreader.utils.Log;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ExpandableGridViewAdapter extends BaseAdapter {
	List<Paper> _mPapers;
	int _currentPage;
	public ExpandableGridViewAdapter(List<Paper> mPapers,int currentPage) {
		_mPapers = mPapers;
		_currentPage = currentPage;
		// TODO Auto-generated constructor stub
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		int paperSize = _mPapers.size();
		int numItem = paperSize - _currentPage* Contants.NUM_ITEM_GRIDVIEW;
		if(numItem < Contants.NUM_ITEM_GRIDVIEW && numItem >0)
		{
			Log.v(""+numItem);
			return numItem;
		}
		// TODO Auto-generated method stub
		Log.v(""+Contants.NUM_ITEM_GRIDVIEW);
		return Contants.NUM_ITEM_GRIDVIEW;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final Context context = parent.getContext();
		View mView = convertView;
		
		
		if(mView == null)
		{				
			LayoutInflater li = LayoutInflater.from(context);
			mView = li.inflate(R.layout.paper_item, null);
		}	
		
		//Log.v("go here" + position + " _currentPage"+ _currentPage);
	
		int itemIndex = _currentPage * Contants.NUM_ITEM_GRIDVIEW + position;
		//v.setOnClickListener(new HomeItemClickListener(itemIndex,context));
		Paper mPaper = _mPapers.get(itemIndex);
		TextView mTitle = (TextView) mView.findViewById(R.id.grid_item_text);
		mTitle.setText(mPaper.getTitle());

		Log.v("title:" + mPaper.getTitle());
		ImageView mImage = (ImageView) mView.findViewById(R.id.grid_item_image);
		ImageLoader mLoaderImage = new ImageLoader(context);
		mLoaderImage.DisplayImage(mPaper.getImage(), mImage);
				
		return mView;
	}

}
